package com.hyy.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyy.api.domain.customer.CstmCustomerProviderChannel;
import com.hyy.api.domain.customer.bo.CstmCustomerProviderChannelBo;
import com.hyy.api.domain.customer.vo.CstmCustomerProviderChannelVo;
import com.hyy.common.core.utils.MapstructUtils;
import com.hyy.common.mybatis.core.page.PageQuery;
import com.hyy.common.mybatis.core.page.TableDataInfo;
import com.hyy.customer.mapper.CstmCustomerProviderChannelMapper;
import com.hyy.customer.service.ICstmCustomerProviderChannelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 客户供应商通道绑定Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-21
 */
@RequiredArgsConstructor
@Service
public class CstmCustomerProviderChannelServiceImpl implements ICstmCustomerProviderChannelService {

    private final CstmCustomerProviderChannelMapper baseMapper;

    /**
     * 查询客户供应商通道绑定
     *
     * @param id 主键
     * @return 客户供应商通道绑定
     */
    @Override
    public CstmCustomerProviderChannelVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询客户供应商通道绑定列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 客户供应商通道绑定分页列表
     */
    @Override
    public TableDataInfo<CstmCustomerProviderChannelVo> queryPageList(CstmCustomerProviderChannelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CstmCustomerProviderChannel> lqw = buildQueryWrapper(bo);
        Page<CstmCustomerProviderChannelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的客户供应商通道绑定列表
     *
     * @param bo 查询条件
     * @return 客户供应商通道绑定列表
     */
    @Override
    public List<CstmCustomerProviderChannelVo> queryList(CstmCustomerProviderChannelBo bo) {
        LambdaQueryWrapper<CstmCustomerProviderChannel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CstmCustomerProviderChannel> buildQueryWrapper(CstmCustomerProviderChannelBo bo) {
        LambdaQueryWrapper<CstmCustomerProviderChannel> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(CstmCustomerProviderChannel::getId);
        lqw.eq(bo.getProviderChannelId() != null, CstmCustomerProviderChannel::getProviderChannelId, bo.getProviderChannelId());
        lqw.eq(bo.getCustomerId() != null, CstmCustomerProviderChannel::getCustomerId, bo.getCustomerId());
        lqw.eq(bo.getWeight() != null, CstmCustomerProviderChannel::getWeight, bo.getWeight());
        lqw.eq(bo.getStatus() != null, CstmCustomerProviderChannel::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增客户供应商通道绑定
     *
     * @param bo 客户供应商通道绑定
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CstmCustomerProviderChannelBo bo) {
        CstmCustomerProviderChannel add = MapstructUtils.convert(bo, CstmCustomerProviderChannel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户供应商通道绑定
     *
     * @param bo 客户供应商通道绑定
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CstmCustomerProviderChannelBo bo) {
        CstmCustomerProviderChannel update = MapstructUtils.convert(bo, CstmCustomerProviderChannel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CstmCustomerProviderChannel entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除客户供应商通道绑定信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public void deleteByCustomerId(Long customerId) {
        LambdaQueryWrapper<CstmCustomerProviderChannel> lqw = Wrappers.lambdaQuery();
        lqw.eq(CstmCustomerProviderChannel::getCustomerId, customerId);
        baseMapper.delete(lqw);
    }

    @Override
    public boolean saveBatch(List<CstmCustomerProviderChannelBo> boList) {
        List<CstmCustomerProviderChannel> convert = MapstructUtils.convert(boList, CstmCustomerProviderChannel.class);
        return  baseMapper.insertBatch(convert);

    }

    @Override
    public List<CstmCustomerProviderChannelVo> selectByCustomerId(Long customerId) {
        LambdaQueryWrapper<CstmCustomerProviderChannel> lqw = Wrappers.lambdaQuery();
        lqw.eq(CstmCustomerProviderChannel::getCustomerId, customerId);
        return baseMapper.selectVoList(lqw);
    }
}
